package com.jt.ktdemo.ui.web

import android.annotation.SuppressLint
import android.os.Bundle
import android.text.TextUtils
import android.view.Window
import android.webkit.WebSettings
import androidx.appcompat.app.AppCompatActivity
import androidx.databinding.DataBindingUtil
import com.jt.ktdemo.R
import com.jt.ktdemo.databinding.ActivityWebBinding

class WebActivity : AppCompatActivity() {

    private lateinit var binding: ActivityWebBinding
    private lateinit var url : String
    private lateinit var title : String

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_web)
        binding.activity = this
        initView()
    }

    @SuppressLint("SetJavaScriptEnabled", "JavascriptInterface")
    private fun initView() {

        url = intent.getStringExtra("module_url").toString()
        title = intent.getStringExtra("module_title").toString()

        binding.infoAWebContent.settings.setSupportZoom(true)
        binding.infoAWebContent.settings.builtInZoomControls = true
        binding.infoAWebContent.settings.displayZoomControls = false
        binding.infoAWebContent.settings.domStorageEnabled = true
        binding.infoAWebContent.settings.cacheMode = WebSettings.LOAD_NO_CACHE
        binding.infoAWebContent.settings.allowFileAccess = true
        binding.infoAWebContent.settings.setGeolocationEnabled(true)
        val ua = binding.infoAWebContent.settings.userAgentString
        binding.infoAWebContent.settings.setUserAgentString("$ua;app/ShiQin@App-Android/CK 2.0")
        //binding.infoAWebContent.getSettings().setUserAgentString("app/ShiQin@App-Android/CK 2.0");
        //binding.infoAWebContent.getSettings().setUserAgentString("app/ShiQin@App-Android");
        binding.infoAWebContent.addJavascriptInterface(this@WebActivity, "android")
        binding.infoAWebContent.settings.javaScriptEnabled = true
        binding.infoAWebContent.settings.javaScriptCanOpenWindowsAutomatically = true
        binding.infoAWebContent.settings.loadWithOverviewMode = true
        binding.infoAWebContent.settings.defaultTextEncodingName = "UTF-8"
        binding.infoAWebContent.settings.setAppCacheEnabled(false)

        if (!TextUtils.isEmpty(url)) {
            binding.infoAWebContent.loadUrl(url)
        }

        if (!TextUtils.isEmpty(title)) {
            binding.tvModuleTitle.text = title
        }


    }
}